__author__ = 'Ben'

import webapp2
from Model import *
import datetime
import logging
from protorpc import messages

class CronTestHandler(webapp2.RequestHandler):
     def get(self):

         #check if dummy not deleted:
        allUsersWithThisNickName = User.query(User.Email == "dummy1@dummy.com")
        if (allUsersWithThisNickName.count(10) == 0):
            user = User(Email = "dummy1@dummy.com",
            Password = "12345",
            Name = "God",
            Gender = "Male",
            Country = "global",
            Grade = 0,
            Birthdate=datetime.date(1980, 1, 1),
            TotalDistance = float(0))
            user.put()
        else:
            user = allUsersWithThisNickName.get()

        #races settings:
        racetime = datetime.datetime.now()+ datetime.timedelta(hours=3,minutes=5)
        year = racetime.year
        month = racetime.month
        day = racetime.day

        only_hour = str( (racetime).hour) #to adjust to GMT+2 bad way!!!!!
        only_minutes = str((racetime).minute) #five minutes from now
        hour = only_hour+":"+only_minutes
        startTime = datetime.datetime(year, month, day, int(hour.split(':')[0]), int(hour.split(':')[1]))
        managerKey = ndb.Key('User', user.key.id())
        privacy = RacePrivacy.Public
        generation = RaceGenerate.Auto

       #race 1 KM
        distance = 0.2
        estimated = 1.5
        tags = "#sprintRun"
        race = Race(StartTime = startTime,
                    Distance = distance,
                    Privacy = privacy,
                    Generation = generation,
                    EstimatedTime = estimated,
                    Tags = tags,
                    Manager = managerKey,
                    Status = RaceStatus.Before)
        race.put()

        #race 2 KM
        distance = 2
        estimated = 12
        tags = "#shortRun"
        race = Race(StartTime = startTime,
                    Distance = distance,
                    Privacy = privacy,
                    Generation = generation,
                    EstimatedTime = estimated,
                    Tags = tags,
                    Manager = managerKey,
                    Status = RaceStatus.Before)
        race.put()

        #race 4 KM
        distance = 4
        estimated = 24
        tags = "#seniors"
        race = Race(StartTime = startTime,
                    Distance = distance,
                    Privacy = privacy,
                    Generation = generation,
                    EstimatedTime = estimated,
                    Tags = tags,
                    Manager = managerKey,
                    Status = RaceStatus.Before)
        race.put()

        #race 5 KM
        distance = 5
        estimated = 30
        tags = "#fastRunnersOnly"
        race = Race(StartTime = startTime,
                    Distance = distance,
                    Privacy = privacy,
                    Generation = generation,
                    EstimatedTime = estimated,
                    Tags = tags,
                    Manager = managerKey,
                    Status = RaceStatus.Before)
        race.put()

        #race 8 KM
        distance = 8
        estimated = 48
        tags = "#parkHayarkon"
        race = Race(StartTime = startTime,
                    Distance = distance,
                    Privacy = privacy,
                    Generation = generation,
                    EstimatedTime = estimated,
                    Tags = tags,
                    Manager = managerKey,
                    Status = RaceStatus.Before)
        race.put()

        #race 10 KM
        distance = 10
        estimated = 60
        tags = "#longRun"
        race = Race(StartTime = startTime,
                    Distance = distance,
                    Privacy = privacy,
                    Generation = generation,
                    EstimatedTime = estimated,
                    Tags = tags,
                    Manager = managerKey,
                    Status = RaceStatus.Before)
        race.put()